package br.com.arquitetura.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import br.com.arquitetura.model.Arquitetura;
import br.com.arquitetura.model.Registrador;
import br.com.arquitetura.model.Sinal;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Main extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private Arquitetura arquitetura = new Arquitetura();
	private JTable jtRegistradores;
	private JTable jtMemoria;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		setTitle("Arquitetura de Computadores - 2012.1");
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		BorderLayout bl = new BorderLayout(0, 0);
		contentPane.setLayout(bl);

		JPanel panelButtons = new JPanel();
		panelButtons.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.add(panelButtons, BorderLayout.NORTH);
		panelButtons.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panelButtons.add(panel, BorderLayout.NORTH);
		panel.setLayout(new CardLayout(0, 0));
		JTextArea jtaEntrada = new JTextArea("Entrada");
		panel.add(jtaEntrada, "name_1341530840583506000");
		jtaEntrada.setLineWrap(true);
		jtaEntrada.setRows(6);
		
		JPanel panel_1 = new JPanel();
		panelButtons.add(panel_1);
		
		JButton btnTodos = new JButton("Toda entrada");
		panel_1.add(btnTodos);
		
		JButton btn1 = new JButton("Instrucao");
		panel_1.add(btn1);
		
		JButton btn2 = new JButton("Microinstrucao");
		panel_1.add(btn2);
		
		JPanel panelBottom = new JPanel();
		panelBottom.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panelBottom, BorderLayout.CENTER);
		panelBottom.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(6, 254, 778, 163);
		panelBottom.add(panel_2);
		JTextArea jtaSaida = new JTextArea("Saída");
		panel_2.add(jtaSaida);
		jtaSaida.setColumns(60);
		jtaSaida.setRows(8);
		jtaSaida.setLineWrap(true);
		jtaSaida.setBackground(Color.LIGHT_GRAY);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 14, 240, 228);
		panelBottom.add(scrollPane);
		
		final JTable jtSinais = new JTable(arquitetura.getSinais().size(), 2);
		scrollPane.setViewportView(jtSinais);
		jtSinais.setPreferredScrollableViewportSize(new Dimension(240, 228));
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(275, 14, 240, 228);
		panelBottom.add(scrollPane_1);
		
		jtRegistradores = new JTable(arquitetura.getRegistradores().size(), 2);
		jtRegistradores.setPreferredScrollableViewportSize(new Dimension(240, 228));
		scrollPane_1.setViewportView(jtRegistradores);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(544, 14, 240, 228);
		panelBottom.add(scrollPane_2);
		
		jtMemoria = new JTable(0, 2);
		jtMemoria.setPreferredScrollableViewportSize(new Dimension(240, 228));
		scrollPane_2.setViewportView(jtMemoria);
		
		setContentPane(contentPane);
		
		reimprimirSinais(jtSinais);
		reimprimirRegistradores(jtRegistradores);
		reimprimirMemoria(jtMemoria);
		
		//Hendlers
		btnTodos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
	}

	private void reimprimirMemoria(JTable jtMemoria) {
		int row = 0;
		DefaultTableModel tmodel = (DefaultTableModel) jtMemoria.getModel();
		tmodel.setNumRows(arquitetura.getMemoria().getAreaUsada().entrySet().size());
		
		for (Entry<String, Integer> e : arquitetura.getMemoria().getAreaUsada().entrySet()) {
			jtMemoria.getModel().setValueAt(e.getKey(), row, 0);
			jtMemoria.getModel().setValueAt(e.getValue(), row, 1);
			row++;
		}
		
	}

	private void reimprimirRegistradores(JTable jtRegistradores) {
		int row = 0;
		DefaultTableModel tmodel = (DefaultTableModel) jtRegistradores.getModel();
		tmodel.setNumRows(arquitetura.getRegistradores().values().size());
		
		for (Registrador r : arquitetura.getRegistradores().values()) {
			jtRegistradores.getModel().setValueAt(r.getNome(), row, 0);
			jtRegistradores.getModel().setValueAt(r.getSaida(), row, 1);
			row++;
		}
	}

	private void reimprimirSinais(JTable jtSinais) {
		int row = 0;
		DefaultTableModel tmodel = (DefaultTableModel) jtSinais.getModel();
		tmodel.setNumRows(arquitetura.getSinais().values().size());
		for (Sinal s : arquitetura.getSinais().values()) {
			jtSinais.getModel().setValueAt(s.getName(), row, 0);
			jtSinais.getModel().setValueAt(s.getSaida(), row, 1);
			row++;
		}
	}
}
